package com.greensam.api.controller.base;

import com.greensam.domain.entity.BaseUserInfo;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

/**
 * @Author: Machengrui
 * @Date: 2024/12/16 14:21
 */
@Controller
public abstract class BaseController {

    /**
     * 获取当前登录用户信息
     */
    protected BaseUserInfo getCurrentUser(){
        SecurityContext context = SecurityContextHolder.getContext();
        Authentication authentication = context.getAuthentication();
        if (authentication.getPrincipal() instanceof BaseUserDetails baseUserDetails){
            return baseUserDetails.getBaseUserInfo();
        }
        return null;
    }
}
